package com.twu.biblioteca;

/**
 * Created by siba on 22/09/14.
 */

import java.util.List;

public class Book {
    private String title;
    private String author;
    private String yearPublished;
    private boolean isCheckedOut;

    public Book(){
        this.title = "";
        this.author = "";
        this.yearPublished = "";
        this.isCheckedOut = false;
    }

    public Book(String[] bookDetailsFromUser) {
        this.title = bookDetailsFromUser[0];
        this.author = bookDetailsFromUser[1];
        this.yearPublished = bookDetailsFromUser[2];
        if(bookDetailsFromUser.length < Constants.BOOK_ATTRIBUTES.length){
            this.isCheckedOut = false;
        }else
            this.isCheckedOut = Boolean.valueOf(bookDetailsFromUser[3]);

    }

    public void setTitle(String title){
        this.title = title;
    }
    public void setAuthor(String author){
        this.author = author;
    }
    public void setYearPublished(String yearPublished){
        this.yearPublished = yearPublished;
    }
    public void setIsCheckedOut(boolean isCheckedOut){
        this.isCheckedOut = isCheckedOut;
    }

    public String getTitle(){
        return this.title;
    }
    public String getAuthor(){
        return this.author;
    }
    public String getYearPublished(){
        return this.yearPublished;
    }
    public boolean getIsCheckedOut(){
        return this.isCheckedOut;
    }

    public static int checkoutBookFromLibrary(Book book){
        Book libraryBook = findBook(book);

        if(libraryBook != null && libraryBook.getIsCheckedOut() == true){
            new BibliotecaMessages(System.out).printMessage(Constants.BOOK_CURRENTLY_UNAVAILABLE);
            return Constants.FAIL;
        }else if (libraryBook != null && libraryBook.getIsCheckedOut() == false){
            new BibliotecaMessages(System.out).printMessage(Constants.CHECKOUT_SUCCESSFUL);
            BookInformation.toggleBookCheckedOutInfo(libraryBook);
            return Constants.SUCCESS;
        }else{
            new BibliotecaMessages(System.out).printMessage(Constants.CHECKOUT_UNSUCCESSFUL);
            return Constants.FAIL;
        }

    }

    public static int returnBookToLibrary(Book book) {
        Book libraryBook = findBook(book);

        if(libraryBook != null && libraryBook.getIsCheckedOut() == true){
            new BibliotecaMessages(System.out).printMessage(Constants.BOOK_RETURNED_SUCCESSFULLY);
            BookInformation.toggleBookCheckedOutInfo(libraryBook);
            return Constants.SUCCESS;
        }else if (libraryBook != null && libraryBook.getIsCheckedOut() == false){
            new BibliotecaMessages(System.out).printMessage(Constants.BOOK_ALREADY_RETURNED);
            return Constants.FAIL;
        }else{
            new BibliotecaMessages(System.out).printMessage(Constants.RETURN_UNSUCCESSFUL);
            return Constants.FAIL;
        }
    }

    public static Book findBook (Book book){
        for(Book libraryBook: BookInformation.getLibraryBooks()){
            if (libraryBook.getTitle().equalsIgnoreCase(book.getTitle()) &&
                    libraryBook.getAuthor().equalsIgnoreCase(book.getAuthor()) &&
                    libraryBook.getYearPublished().equalsIgnoreCase(book.getYearPublished())){
                return libraryBook;
            }
        }
        return null;
    }
}
